# -*- coding: utf-8 -*-

# Scrapy settings for GlobalLawSpider project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import datetime

BOT_NAME = 'GlobalInternationTreaSpider'

SPIDER_MODULES = ['GlobalInternationTreaSpider.spiders', 'GlobalInternationTreaSpider.Philippinesspiders', 'GlobalInternationTreaSpider.Wipo']
# NEWSPIDER_MODULE = 'GlobalLawSpider.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'GlobalLawSpider (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 8    # 下载器的最大并行数量默认为32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 5  # 下载器从同一网站下载连续网页的等待时间,限制爬行速度保证将pdf完全下载
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16   # 对任何单个域的并发请求的最大数量，默认为16
# CONCURRENT_REQUESTS_PER_IP = 16   # 对任何单个ip执行的并发请求最大数量，默认为16
# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'GlobalLawSpider.middlewares.GloballawSpiderSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    'GlobalInternationTreaSpider.middlewares.GlobalinternationTreaspiderDownloaderMiddleware': 543,
    # 'GlobalLawSpider.middlewares.ProxyMiddleware': 545,   # 代理IP中间件
    'GlobalInternationTreaSpider.middlewares.GLobalLawWebdriverDownloaderMiddleware': 547,  # 调用谷歌浏览器的中间件
    # 'GlobalLawSpider.middlewares.UrlRedisFilter': 560,      # 基于redis的增量爬取中间件
}

SPIDER_MIDDLEWARES = {
    'GlobalInternationTreaSpider.middlewares.GlobalinternationTreaSpiderMiddleware': 544,
    'GlobalInternationTreaSpider.middlewares.UrlRedisFilter': 545,
}

# 这是mysql数据库的连接属性
MYSQL_CONNECT = {
    'MYSQL_HOST': '114.115.219.219',
    'MYSQL_PORT': 3306,
    'MYSQL_DB': 'globallegaldatabase',
    'MYSQL_USER': 'root',
    'MYSQL_PASSWD': 'xinanzhengfadaqxueapp03!',
    'MYSQL_CHARSET': 'utf8',
    'MYSQL_TABLE': 'treaty'  # 这是数据库存储数据的表
}

# 这是redis数据库信息
REDIS_CONNECT = {
    'REDIS_HOST': '114.115.219.219',
    'REDIS_PORT': 6379,
    'REDIS_PASSWD': 'CNKIcnki614',
    'REDIS_DB': '1',
    'REDIS_KEY': 'Treaty'
}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'GlobalInternationTreaSpider.pipelines.MyFilesPipeline': 1,     # 构造下载pdf文件的文件名以及路径
    'GlobalInternationTreaSpider.pipelines.SingaporespiderPipeline': 300,   # 将网页信息以json格式保存
    'GlobalInternationTreaSpider.pipelines.MydetaHTMLpipeline': 600,    # 将detail中的原文内容转为pdf格式保存
    'GlobalInternationTreaSpider.pipelines.MydetaHTMLUrlpipeline': 620,    # 将detailUrl网址中的内容转为pdf格式保存
   'GlobalInternationTreaSpider.pipelines.Myhtmlpdfpipeline': 650,    # 将pdf格式的dowloadUrl网址的信息存入本地pdf文件
#    'GlobalLawSpider.pipelines.MyHTMLpipeline': 700,    # 将摘要的Html内容转为pdf格式保存
    'GlobalInternationTreaSpider.pipelines.MySqlpipeline': 720,     # 将网页爬取的数据存入sql
}

time_now = str(datetime.datetime.now().year) + '-' + str(datetime.datetime.now().month)
FILES_STORE = 'D:/GlobalLawFiles/%s/Treaty' % time_now  # 这里是存储下载文件的路径
DOWNLOAD_TIMEOUT = 800   # 超时时间限制

# 这是存储代理IP的信息
PROXIES = ['https://182.34.33.135:9999', 'https://223.199.16.158:9999', 'https://114.239.146.152:808',
           'https://180.122.148.179:22957', 'https://61.145.182.27:53281', 'https://223.199.16.158:9999',
           'https://117.30.112.251:9999', 'https://222.90.110.194:8080', 'https://60.167.20.206:808',
           'https://60.167.132.250:37543', 'https://114.99.2.127:808', 'https://117.88.5.230:3000']

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

# DUPEFILTER_DEBUG = True #记录全部重复
FEED_EXPORT_ENCODING = 'UTF-8'
